package com.addremoveitemtest;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageButton;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btn_more)
    ImageButton btnMore;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private ArrayList<String> strList0=new ArrayList<>();
    private ItemFragment[] ItemFragments;
    private PageAdapter pageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFragment(strList0);
    }

    private void initFragment(ArrayList<String> strList0) {
        ItemFragments=new ItemFragment[strList0.size()];
        for (int i = 0; i < ItemFragments.length; i++) {
            ItemFragments[i]=ItemFragment.newInstance(strList0.get(i));
        }
        String[] title=new String[strList0.size()];
        for (int i = 0; i < title.length; i++) {
            title[i]=strList0.get(i);
        }
        FragmentManager supportFragmentManager = getSupportFragmentManager();

        pageAdapter = new PageAdapter(supportFragmentManager, this, title, ItemFragments);
        viewPager.setAdapter(pageAdapter);
        tabLayout.setupWithViewPager(viewPager);

    }

    @OnClick(R.id.btn_more)
    public void onViewClicked() {
        startActivityForResult(new Intent(this,AddRemove.class),100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100&&resultCode==101) {
            ArrayList<String> strListCur = data.getStringArrayListExtra("strListCur");
            strList0.addAll(strListCur);
            initFragment(strList0);
            pageAdapter.notifyDataSetChanged();
        }
    }
}
